<?php
declare(strict_types=1);

namespace App\Model;



use Hyperf\Database\Model\Events\Saving;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Database\Model\SoftDeletes;

/**
 * @property $id
 * @property $name
 * @property $mobile
 * @property $created_at
 * @property $updated_at
 */
class Config extends Model
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    // protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected array $hidden = ['created_at', 'updated_at'];

    #[Cacheable(prefix: "getConfigsWithChildren", ttl: 100, listener: "getConfigsWithChildren-update")]
    public static function getConfigsWithChildren($pid){
        return self::with('children')->where('pid', $pid)->orderBy('listorder', 'asc')->get();
    }

    public function children(){
        return $this->hasMany(Config::class, 'pid', 'id')->orderByRaw('listorder asc');
    }

    public static function loadData($pid = 0) {
        $row = Model::where('pid', $pid)->get();
        if($row->isEmpty()){
            return [];
        }
        foreach ($row as $key => $value) {
            $value->children = self::loadData($value->id);
        }
        return $row;
    	
    }



}